<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

//后台模块表
class UserModel extends Model
{
    //定义一个表名
    protected $table = 'users';
    //定义主键
    protected $primaryKey = 'id';
	protected $guarded = [];

    //权限
    public function listAll()
    {
        $list = $this->orderBy('id', 'desc')->get();
        return $list;
    }
	static function Dolist($uid)
	{
		$list = UserModel::where('id',$uid)->first();
		$data['id'] = $list['id'];
		$data['user'] = $list['user'];
		if($list['picname']){
			$data['picname'] = "http://".$_SERVER['HTTP_HOST'].$list['picname'];
		}else{
			$data['picname'] = "http://".$_SERVER['HTTP_HOST']."/user.png";
		}
		//用户第三方登录
		$party = UserLoginModel::where('uid',$uid)->first();

		if(empty($list->picname)){
			if(empty($party['picname'])){
				$list->picname = "http://".$_SERVER['HTTP_HOST']."/user.png";
			}else{
				$list->picname = $party['picname'];
			}
		}else{
			$list->picname = "http://".$_SERVER['HTTP_HOST'].$list->picname;
		}


		$data['invite_code'] = $list['invite_code'];
		$data['is_vip'] = $list['is_vip'];
		$data['in_date'] = '';
		if($list['is_vip'] == 1){
			$data['in_date'] = StoreModel::where('uid',$list['id'])->value('end');
		}
		$data['is_auth'] = $list['is_auth'];
		$data['pid'] = $list['pid'];
		$data['phone'] = $list['phone'];
		$data['email'] = $list['email'];
		$data['state'] = $list['state'];
		$data['login_type'] = $list['login_type'];
		$data['password'] = $list['password'];
		$data['paypass'] = $list['paypass'];
		$data['is_auth'] = $list['is_auth'];
		$data['level'] = $list['level'];
		return $data;
	}

	function getPuserAttribute($value)
    {
        return UserModel::where('id', $this->pid)->value('user') ? UserModel::where('id', $this->pid)->value('user') : '无';
    }
	function getMoneyAttribute($value)
    {
        return UserWalletModel::where('uid', $this->id)->value('money') ? UserWalletModel::where('uid', $this->id)->value('money') : '无';
    }
	function getLockmoneyAttribute($value)
    {
        return UserWalletModel::where('uid', $this->id)->value('lock_money') ? UserWalletModel::where('uid', $this->id)->value('lock_money') : '无';
	}
	
	function getLevelNameAttribute($value)
    {
		$arr = ['0'=>'普通用户','1'=>'银卡','2'=>'金卡'];
		$level = $arr[$this->level];
		return $level;
	}
	
    function getHeadpicAttribute($value){
    	if($this->picname){
    		return "http://".$_SERVER['HTTP_HOST'].$this->picname;
		}else{
    		return "http://".$_SERVER['HTTP_HOST']."user.png";
		}
	}

	//生成token
	public function generateToken()
	{
		$this->token = str_random(128);
		$this->save();

		return $this->token;
	}

     //启用禁用
    public function updateState($id,$state)
    {
        $res = $this->find($id);
        if($state == 1){
            $msg = '更改用户【'.$res['user'].'】(id:'.$res['id'].')状态为【启用】';
        }
        if($state == 2){
            $msg = '更改用户【'.$res['user'].'】(id:'.$res['id'].')状态为【禁用】';
        }

        $arr = $this->where("id",$id)->update(['state' => $state]);
        if($arr){
            //添加日志
            AdminLogsModel::addLog($msg);
            $returnArr['code'] = 1;
            $returnArr['msg'] = "修改成功";
        }else{
            $returnArr['code'] = 2;
            $returnArr['msg'] = "修改失败";
        }
        return $returnArr;
    }

    //定义26字母
	static function letter($uid,$keywords){
		$letter = [
			[
				'letter' => 'A',
				'user' => UserModel::letter_re('A',$uid,$keywords),
			],
			[
				'letter' => 'B',
				'user' => UserModel::letter_re('B',$uid,$keywords),
			],
			[
				'letter' => 'C',
				'user' => UserModel::letter_re('C',$uid,$keywords),
			],
			[
				'letter' => 'D',
				'user' => UserModel::letter_re('D',$uid,$keywords),
			],
			[
				'letter' => 'E',
				'user' => UserModel::letter_re('E',$uid,$keywords),
			],
			[
				'letter' => 'F',
				'user' => UserModel::letter_re('F',$uid,$keywords),
			],
			[
				'letter' => 'G',
				'user' => UserModel::letter_re('G',$uid,$keywords),
			],
			[
				'letter' => 'H',
				'user' => UserModel::letter_re('H',$uid,$keywords),
			],
			[
				'letter' => 'I',
				'user' => UserModel::letter_re('I',$uid,$keywords),
			],
			[
				'letter' => 'J',
				'user' => UserModel::letter_re('J',$uid,$keywords),
			],
			[
				'letter' => 'K',
				'user' => UserModel::letter_re('K',$uid,$keywords),
			],
			[
				'letter' => 'L',
				'user' => UserModel::letter_re('L',$uid,$keywords),
			],
			[
				'letter' => 'M',
				'user' => UserModel::letter_re('M',$uid,$keywords),
			],
			[
				'letter' => 'N',
				'user' => UserModel::letter_re('N',$uid,$keywords),
			],
			[
				'letter' => 'O',
				'user' => UserModel::letter_re('O',$uid,$keywords),
			],
			[
				'letter' => 'P',
				'user' => UserModel::letter_re('P',$uid,$keywords),
			],
			[
				'letter' => 'Q',
				'user' => UserModel::letter_re('Q',$uid,$keywords),
			],
			[
				'letter' => 'R',
				'user' => UserModel::letter_re('R',$uid,$keywords),
			],
			[
				'letter' => 'S',
				'user' => UserModel::letter_re('S',$uid,$keywords),
			],
			[
				'letter' => 'T',
				'user' => UserModel::letter_re('T',$uid,$keywords),
			],
			[
				'letter' => 'U',
				'user' => UserModel::letter_re('U',$uid,$keywords),
			],
			[
				'letter' => 'V',
				'user' => UserModel::letter_re('V',$uid,$keywords),
			],
			[
				'letter' => 'W',
				'user' => UserModel::letter_re('W',$uid,$keywords),
			],
			[
				'letter' => 'X',
				'user' => UserModel::letter_re('X',$uid,$keywords),
			],
			[
				'letter' => 'Y',
				'user' => UserModel::letter_re('Y',$uid,$keywords),
			],
			[
				'letter' => 'Z',
				'user' => UserModel::letter_re('Z',$uid,$keywords),
			],
			[
				'letter' => '#',
				'user' => UserModel::letter_re('#',$uid,$keywords),
			],
		];

		return $letter;
	}

	static function letter_re($letter,$uid,$words){
    	$data = [];
    	$keywords = '';
    	if($words){
			$keywords = $words;
		}
		$list = UserModel::where('pid', $uid)->where('head',$letter)->where('user',"like","%".$keywords."%")->orderBy(DB::raw('convert(`user` using gbk)'))->select('id','nickname','level','picname','is_auth','user','login_type','is_vip','phone','email')->get()->toArray();
		foreach($list as $k=>$v){
			//头像
			if($v['picname']){
				$data[$k]['headpic'] = "http://".$_SERVER['HTTP_HOST'].$v['picname'];
			}
			$data[$k]['headpic'] = "http://" . $_SERVER['HTTP_HOST'] . "/user.png";
			$data[$k]['user'] = $v['user'];
			if($v['nickname']){
				$data[$k]['user'] = $v['nickname'];
			}
			//账号
			if($v['login_type'] == 1){
				$data[$k]['username'] = $v['phone'];
			}else{
				$data[$k]['username'] = $v['email'];
			}
			//是否是VIP
			$data[$k]['is_vip'] = $v['is_vip'];
			$data[$k]['id'] = $v['id'];
			$data[$k]['is_auth'] = $v['is_auth'];
			$data[$k]['level'] = $v['level'];
		}
		return $data;
	}

	//获取下级信息
	static function getList($uid,$words){
		$data = [];
		$keywords = '';
		if($words){
			$keywords = $words;
		}
		$list = UserModel::where('pid', $uid)->where('user',"like","%".$keywords."%")->select('id','nickname','picname','user','login_type','is_vip','phone','email','is_auth','level')->get()->toArray();
		foreach($list as $k=>$v){
			//用户是否第三方
			$party = UserLoginModel::where('uid',$v['id'])->first();
			if(empty($v['picname'])){
				if(empty($party['picname'])){
					$data[$k]['headpic'] = "http://".$_SERVER['HTTP_HOST']."/user.png";
				}else{
					$data[$k]['headpic'] = $party['picname'];
				}

			}else{
				$data[$k]['headpic'] = "http://".$_SERVER['HTTP_HOST'].$v['picname'];
			}


			// $data[$k]['headpic'] = "http://" . $_SERVER['HTTP_HOST'] . "/user.png";
			// //头像
			// if($v['picname']){
			// 	$data[$k]['headpic'] = "http://".$_SERVER['HTTP_HOST'].$v['picname'];
			// }
			
			$data[$k]['user'] = $v['user'];
			if($v['nickname']){
				$data[$k]['user'] = $v['nickname'];
			}
			//账号
			if($v['login_type'] == 1){
				$data[$k]['username'] = $v['phone'];
			}else{
				$data[$k]['username'] = $v['email'];
			}
			//是否是VIP
			$data[$k]['is_vip'] = $v['is_vip'];
			$data[$k]['id'] = $v['id'];
			$data[$k]['is_auth'] = $v['is_auth'];
			$data[$k]['level'] = $v['level'];
		}
		return $data;
	}

	//身份证=真实姓名
	function getTruenameAttribute(){
		$name = '无';
		$list = UserAuthModel::where('uid',$this->id)->first();
		if($list){
			$name = $list['name'];
		}
		return $name;
	}

	//身份证=真实姓名
	function getIdcardAttribute(){
		$idcard = '无';
		$list = UserAuthModel::where('uid',$this->id)->first();
		if($list){
			$idcard = $list['idcard'];
		}
		return $idcard;
	}


}
